/*
  * 定义全局变量
  **/
var global = {
    location:"http://a.freei.cc"
}
/*
*获取当前页地址
* 加载css样式
*/
function getPathname(){
    // 获取当前页面
    var winPath = window.location.pathname;
    switch(winPath){
        case "/Alasun/index.html":
            $(".header-link").find("li").eq(0).addClass("active").siblings().removeClass("active");
            break;
        case "/Alasun/speech.html":
            $(".header-link").find("li").eq(1).addClass("active").siblings().removeClass("active");
            break;
        case "/Alasun/honor.html":
            $(".header-link").find("li").eq(1).addClass("active").siblings().removeClass("active");
            break;
        case "/Alasun/structure.html":
            $(".header-link").find("li").eq(1).addClass("active").siblings().removeClass("active");
            break;
        case "/Alasun/culture.html":
            $(".header-link").find("li").eq(1).addClass("active").siblings().removeClass("active");
            break;
        case "/Alasun/news-jtxw.html":
            $(".header-link").find("li").eq(2).addClass("active").siblings().removeClass("active");
            break;
        case "/Alasun/news-hydt.html":
            $(".header-link").find("li").eq(2).addClass("active").siblings().removeClass("active");
            break;
        case "/Alasun/news-details.html":
            $(".header-link").find("li").eq(2).addClass("active").siblings().removeClass("active");
            break;
        case "/Alasun/production-base.html":
            $(".header-link").find("li").eq(3).addClass("active").siblings().removeClass("active");
            break;
        case "/Alasun/development.html":
            $(".header-link").find("li").eq(3).addClass("active").siblings().removeClass("active");
            break;
        case "/Alasun/detecting-system.html":
            $(".header-link").find("li").eq(4).addClass("active").siblings().removeClass("active");
            break;
        case "/Alasun/sale.html":
            $(".header-link").find("li").eq(5).addClass("active").siblings().removeClass("active");
            break;
        case "/Alasun/joinus.html":
            $(".header-link").find("li").eq(6).addClass("active").siblings().removeClass("active");
            break;
        case "/Alasun/contact.html":
            $(".header-link").find("li").eq(7).addClass("active").siblings().removeClass("active");
            break;
    }
}
function getAjax(url,callback,data){
    $.ajax({
        url:url,
        data:data,
        dataType: "json",
        success:function (res) {
            callback(res)
        },
        error:function () {
            console.log(err);
        }
    })
}
function getSearch(){
    var searchUrl = window.location.search;
    searchUrl = searchUrl.substring(1,searchUrl.length);
    var urlParam = searchUrl.split("&");
    return urlParam;
}

$(document).ready(function () {
    // console.log("main.js");
    //加载头尾
    $("#header").load("template/header.html",function () {
        getPathname();
    });
    $("#footer").load("template/footer.html");
    // 小图切换
    // var productIntroBoxW = $(".product-intro-item").length*180;
    // var productIntroBox = $(".product-intro-box");
    // // console.log(productIntroBoxW);
    // $(".product-intro-nextbtn").click(function () {
    //     // console.log("产品介绍点击事件");
    //     var productIntroBoxML = productIntroBox.css("margin-left");
    //     if(630-productIntroBoxW >= parseInt(productIntroBoxML)){
    //         productIntroBox.css("margin-left","0px");
    //     }else{
    //         // console.log(parseInt(productIntroBoxML));
    //         productIntroBox.css("margin-left", (parseInt(productIntroBoxML) - 180) + "px");
    //     }
    // })
    // var timer = setInterval(function () {
    //     var productIntroBoxML = productIntroBox.css("margin-left");
    //     if(630-productIntroBoxW >= parseInt(productIntroBoxML)){
    //         productIntroBox.css("margin-left","0px");
    //     }else{
    //         // console.log(parseInt(productIntroBoxML));
    //         productIntroBox.css("margin-left", (parseInt(productIntroBoxML) - 180) + "px");
    //     }
    // },3000)



    //首页新闻动态与行业动态切换
    var newsNavs = $(".news-nav li");
    var newsContainers = $(".news-container");
    newsNavs.on("mouseover",function () {
        newsNavs.removeClass("active");
        $(this).addClass("active");
        console.log($(this).index());
        newsContainers.css("display","none");
        newsContainers.eq($(this).index()).css("display","block");
    })
    //搜索按钮
    var searchBtn = $(".header-search");
    var searchBox = $(".header-search-box");
    searchBtn.on("click",function () {
        var searchBoxW = (searchBox.width() == 0)?"130px":0;
        searchBox.css("width",searchBoxW);
    })
    //集团新闻
    var indexJTXWUrl = global.location+"/Api/New/first_jtxw_three";
    getAjax(indexJTXWUrl,function(res) {
        var html = '';
        var html01 = '';
        html += `
                     <a href="news-details.html?id=${res[0].id}&fromPage=0" target="_blank">
                            <p class="news-headline-title">
                                <span class="headline-date">${res[0].date}</span>
                                <span class="headline-title">${res[0].title}</span><!--待完成：将汉字转为。。。-->
                            </p>
                            <p class="news-headline-content">${res[0].sub_content}</p>
                        </a>
                    `;
        $("#jtxw .news-headline").html(html);
        for(var i=1;i<res.length;i++){
            html01 += `
                             <li class="news-item"><a href="news-details.html?id=${res[i].id}&fromPage=0" target="_blank"><span class="news-item-title">${res[i].title}</span><span class="news-item-date">${res[i].date}</span></a></li>
                        `;
        }
        $("#jtxw .news-items").html(html01);
    });
    //行业动态
    var indexHYDTUrl = global.location+"/Api/New/first_hydt_three";
    getAjax(indexHYDTUrl,function (res) {
        var html = '';
        var html01 = '';
        html += `
                    <a href="news-details.html?id=${res[0].id}&fromPage=1" target="_blank">
                            <p class="news-headline-title">
                                <span class="headline-date">${res[0].date}</span>
                                <span class="headline-title">${res[0].title}</span><!--待完成：将汉字转为。。。-->
                            </p>
                            <p class="news-headline-content">${res[0].sub_content}</p>
                    </a>
                    `;
        $("#hydt .news-headline").html(html);
        for(var i=1;i<res.length;i++){
            html01 += `
                             <li class="news-item"><a href="news-details.html?id=${res[i].id}&fromPage=1" target="_blank"><span class="news-item-title">${res[i].title}</span><span class="news-item-date">${res[i].date}</span></a></li>
                        `;
        }
        $("#hydt .news-items").html(html01);
    })
    //banner-img
    var bannerUrl = global.location+"/Api/New/get_banner";
    getAjax(bannerUrl,function (res) {
        var html = "";
        for(var i=0;i<res.length;i++){
            html+=`
                <li class="slider-item" style="background-image:url(${res[i].banner_img});">
                        <!--<a href="#"></a>-->
                </li> `
        }
        $(".slider-items").html(html);
        // console.log(1);
    })
/*
 *轮播
*/
})
// news page
